﻿using System;
using UnityEngine;

/// <summary>
/// MonoBehaviour 类扩展
/// </summary>
public static class MonoBehaviourExtension
{

    public static void SetActive(this MonoBehaviour monoBehaviour, bool isActive)
    {
        monoBehaviour.gameObject.SetActive(isActive);
    }

    /// <summary>
    /// 获取或者添加组件
    /// </summary>
    /// <typeparam name="T">组件类型</typeparam>
    /// <param name="gameObject"></param>
    /// <returns></returns>
    public static T GetOrAddComponent<T>(this MonoBehaviour monoBehaviour) where T : Component
    {
        return Utility.Component.GetOrAddComponent<T>(monoBehaviour.gameObject);
    }

    /// <summary>
    /// 获取或者添加 MonoBehaviour 下指定路径的组件
    /// </summary>
    /// <typeparam name="T">组件类型</typeparam>
    /// <param name="gameObject"></param>
    /// <param name="path">monoBehaviour 下指定路径</param>
    /// <returns></returns>
    public static T GetOrAddComponent<T>(this MonoBehaviour monoBehaviour, string path) where T : Component
    {
        return Utility.Component.GetOrAddComponent<T>(monoBehaviour.gameObject, path);
    }

    public static Coroutine Delay(this MonoBehaviour monoBehaviour, float time, Action action)
    {
        return monoBehaviour.StartCoroutine(Utility.Coroutine.Delay(time, action));
    }
}
